using System;
using System.Collections.Generic;
using System.Text;
using System.Management.Automation;
using System.Collections;

namespace PoshXmpp
{
    public abstract class XmppCommandBase : PSCmdlet
    {
        #region Parameters
        [Parameter( Position=100,
            Mandatory = false,
            ValueFromPipelineByPropertyName = true,
            HelpMessage = "XmppClient to use")]
        [Alias("XmppClient")]
        public XmppClient Client { get; set; }

        [Parameter( 
            Mandatory = false,
            HelpMessage = "Pass through the Client parameter")]
        public SwitchParameter Passthru { get; set; }
        #endregion

        protected bool _clientFromArgument = false;
        protected bool _clientFromPipeline = false;

        protected override void BeginProcessing()
        {
            base.BeginProcessing();
            _clientFromArgument = (Client != null);
        }

        private bool GetClient()
        {
            if (Client == null)
            {
                try
                {
                    PSVariable PoshXmppClient = this.SessionState.PSVariable.Get("PoshXmppClient");
                    if (PoshXmppClient != null)
                    {
                        if (PoshXmppClient.Value is PSObject)
                        {
                            Client = (PoshXmppClient.Value as PSObject).BaseObject as XmppClient;
                        } else 
                        Client = (PoshXmppClient.Value as XmppClient);
                    }
                    else
                    {
                        ThrowTerminatingError(new ErrorRecord(new NullReferenceException("XmppClient variable not set or set incorrectly"),
                                                                "No XmppClient", ErrorCategory.ObjectNotFound, Client));
                    }
                }
                catch
                {
                    ThrowTerminatingError(new ErrorRecord(new NullReferenceException("XmppClient variable not set or set incorrectly"),
                                                            "No XmppClient", ErrorCategory.ObjectNotFound, Client));
                }
                return false;
            }
            else
            {
                 return true;
            }
        }
        
        protected override void ProcessRecord()
        {
            base.ProcessRecord();
            
            if(!_clientFromArgument) _clientFromPipeline = GetClient();
            if (Passthru.ToBool() && _clientFromPipeline) { WriteObject(Client); }


            if (!Client.Authenticated || !(
                Client.XmppConnectionState == agsXMPP.XmppConnectionState.Connected ||
                Client.XmppConnectionState == agsXMPP.XmppConnectionState.SessionStarted))
            {
                ThrowTerminatingError(new ErrorRecord(new InvalidOperationException("Can't send message while not connected"),
                                                        "XmppClient Disconnected", ErrorCategory.OperationStopped, Client));
            }

        }
     }
}
